using mvc.Models;
using mvc.Db;
using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
namespace mvc.IR
{
    public class ERe<T> : IRe<T> where T : BaseEntity
    {
        private readonly Dbcon _db;
        private readonly DbSet<T> _t;

        public  IQueryable<T> table
        {
            get
            {
                return _t.AsQueryable();
            }
        }
        public ERe()
        {
            _db = new Dbcon();
            _t = _db.Set<T>();
        }
        public void Delete(int id)
        {
            var entity = _t.Where(x=>x.Id==id).FirstOrDefault();
            entity.IsDeleted=true;
            entity.IsActived=false;
            _t.Update(entity);
            _db.SaveChanges();
        }

        public void Insert(T entity)
        {
            entity.IsDeleted=false;
            entity.CreatedTime=System.DateTime.Now;
            entity.IsActived=true;
            entity.DisplayOrder = 0;
            entity.UpdatedTime = System.DateTime.Now;
            entity.Creator=0;
            _t.Add(entity);
            _db.SaveChanges();
            
        }

        public void Update(T entity)
        {
            entity.UpdatedTime = System.DateTime.Now;
            _t.Update(entity);
            _db.SaveChanges();
        }
    }
}